<?php

class Application_Model_Dao_Speciality extends Zend_Db_Table_Abstract
{

	protected $_name = 'specialities';
    protected $_primary = 'id';
    	
	public function init(){
    	
    }
	public function getAllSpeciality(){
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$select = $dbAdapter->select()->from('specialities')->joinLeft('majors', 'specialities.major_id = majors.id', array('major_designation' => 'designation'));
		$result = $dbAdapter->fetchAll($select);
		return $result;
	}
 	public function getAll(){
		$result = $this->fetchAll();
		if( !$result )
			throw new Exception("Wystąpił błąd przy pobieraniu danych z bazy. Proszę spróbować jeszcze raz. Jeśli problem będzie się powtarzać, prosimy skontaktować się z administratorem.");	
		return $result;
    }
public function getRowSpeciality($id){
		$dbAdapter = Zend_Db_Table::getDefaultAdapter();
		$select = $dbAdapter->select()->from('specialities')->where('specialities.id = \''.$id.'\'')->joinLeft('majors', 'specialities.major_id = majors.id', array('major_designation' => 'designation'));
		$result = $dbAdapter->fetchAll($select);
		return $result;
	}

	public function getSpecialityByID($id){
		$id = (int)$id;
		$row = $this->fetchRow('id = '.$id);
		if (!$row)
			throw new Exception("Specialność o ID $id nie istnieje.");
		return $row->toArray();
	}
	public function getSpecialityByDesignation($designation){
		$row = $this->fetchRow('designation = '.$designation);
		if (!$row)
			throw new Exception("Specialność o nazwie $designation nie istnieje.");
		return $row->toArray();
	}
  	public function saveSpeciality($speciality) {
  	$data = array(
			'major_id' => $speciality->getMajor(),
			'designation' => $speciality->getDesignation(),
		);
		$result = $this->insert($data);
     return $result;
    }
	public function updateSpeciality($speciality) {
		$data = array(
			'major_id' => $speciality->getMajor(),
			'designation' => $speciality->getDesignation(),
		);
		$result = $this->update($data, 'id = '. (int)$speciality->getId());
		return $result;
	}
public function deleteSpeciality($speciality){
		$result = $this->delete('id =' . (int)$speciality->getId());
		return $result;
	}
  	public function getToSelect(){
    	$array = $this->getAll()->toArray();
    	$specialities[''] = "wybierz";
    	foreach ($array as $option) {
     	$specialities[ $option['id'] ] = $option['designation' ] ;
     }
     return $specialities;
    }
        
}
   